class Shop::SettlesController < ShopController
  def index
    @settles = current_shoper.shop.settles.order("created_at DESC").page(params[:page]).per(params[:per])
  end
  
  def show
    @settle = Settle.find params[:id]
  end

  def new
    @settle = Settle.new
  end

  def create
    @settle = Settle.new settle_params
    @settle.shop_id = current_shoper.shop_id
    @settle.shoper_id = current_shoper.id
    @settle.state = :pending
    if @settle.save
      redirect_to shop_settles_path, notice: "申请结算成功！"
    else
      redirect_to shop_settles_path, notice: "申请结算失败！"
    end
  end
  
  def destroy
    @settle = Settle.find params[:id]
    if @settle.pending? || @settle.checked?
      if @settle.update_attributes(state: :cancel, remark: "商家主动取消结算.")
        redirect_to shop_settles_path, notice: "取消结算成功."
      else
        redirect_to shop_settles_path, notice: "取消结算失败."
      end
    else
      redirect_to shop_settles_path, notice: "不能取消结算."
    end
  end

  private

  def settle_params
    params.require(:settle).permit(:fee, :remark)
  end
end
